/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.scs.controller;

import com.wisnu.scs.error.ErrorMessage;
import com.wisnu.scs.event.MainFrameListener;
import com.wisnu.scs.util.MySQLUtil;
import com.wisnu.scs.view.ConfigView;
import com.wisnu.scs.view.DbBarangKeluarView;
import com.wisnu.scs.view.DbBarangMasukView;
import com.wisnu.scs.view.DbBarangView;
import com.wisnu.scs.view.MainFrame;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ConnectException;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Wisnu Wardoyo
 */
public class MainFrameController {

    private DbBarangView barangView;
    private DbBarangMasukView barangMasukView;
    private DbBarangKeluarView barangKeluarView;

    private MainFrameListener listener;

    public void setListener(MainFrameListener listener) {
        this.listener = listener;
    }

    public void showDbBarang(MainFrame view) {
        barangView = new DbBarangView();
        try {
            barangView.loadDatabase();
        } catch (ErrorMessage ex) {
            JOptionPane.showMessageDialog(view, ex.getMessage());
        }
        view.getMainScrollPane().setViewportView(barangView);
    }

    public void showDbBarangMasuk(MainFrame view) {
        barangMasukView = new DbBarangMasukView();
        try {
            barangMasukView.loadTable();
            barangMasukView.loadDatabase();
        } catch (ErrorMessage ex) {

        }
        view.getMainScrollPane().setViewportView(barangMasukView);
    }

    public void showDbBarangKeluar(MainFrame view) {
        barangKeluarView = new DbBarangKeluarView();
        try {
            barangKeluarView.loadTable();
            barangKeluarView.loadDatabase();
        } catch (ErrorMessage ex) {

        }
        view.getMainScrollPane().setViewportView(barangKeluarView);
    }

    public void connectionCheck(MainFrame frame) {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("./service/Hibernate.properties"));
            MySQLUtil.getConnection(properties);
            frame.setProperties(properties);
            frame.setIsConnected(true);
            fireIsConnect();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(frame, new Object[]{
                "Terjadi error pada database dengan pesan :",
                "Hibernate.properties not found",
                "Coba tanya Wisnu, programnya ga bisa jalan tanpa ini!"
            });
            System.exit(0);
        } catch (SQLException ex) {
            if (ex.getErrorCode() == 0) {
                JOptionPane.showMessageDialog(frame, new Object[]{
                    "Terjadi error pada database dengan pesan :",
                    "Server MySQL tidak ditemukan, atau",
                    "URL Database salah, Silahkan nyalakan service MySQL",
                    "Atau Konfigurasi dengan ALT + K, lalu reconnect!"
                });
                frame.setIsConnected(false);
                fireIsConnect();
            } else if (ex.getErrorCode() == 1049) {
                try {
                    frame.setProperties(properties);
                    MySQLUtil.createDatabase(properties);
                    frame.setIsConnected(true);
                    fireIsConnect();
                } catch (SQLException ex1) {
                    System.out.println(ex1.getMessage());
                    frame.setIsConnected(false);
                    fireIsConnect();
                }
            } else if (ex.getErrorCode() == 1044) {
                JOptionPane.showMessageDialog(frame, new Object[]{
                    "Terjadi error pada database dengan pesan :",
                    "Username database salah"
                });
                frame.setIsConnected(false);
                fireIsConnect();
            } else if (ex.getErrorCode() == 1045) {
                JOptionPane.showMessageDialog(frame, new Object[]{
                    "Terjadi error pada database dengan pesan :",
                    "Password database salah"
                });
                frame.setIsConnected(false);
                fireIsConnect();
            } else {
                System.out.println(ex.getErrorCode());
                frame.setIsConnected(false);
                fireIsConnect();
            }
        }
    }

    protected void fireIsLogin() {
        if (listener != null) {
            listener.isLogin();
        }
    }

    protected void fireIsConnect() {
        if (listener != null) {
            listener.isConnect();
        }
    }

    public void editConfiguration(MainFrame frame) {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("./service/Hibernate.properties"));
        } catch (FileNotFoundException ex) {

        } catch (IOException ex) {

        }
        ConfigView configView = new ConfigView();
        configView.getTxtURL().setText(properties.getProperty("hibernate.connection.url"));
        configView.getTxtUser().setText(properties.getProperty("hibernate.connection.username"));
        configView.getTxtPass().setText(properties.getProperty("hibernate.connection.password"));

        int result = JOptionPane.
                showConfirmDialog(null, configView, "KONFIGURASI", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE);

        if (result == JOptionPane.YES_OPTION) {
            properties.setProperty("hibernate.connection.url", configView.getTxtURL().getText());
            properties.setProperty("hibernate.connection.username", configView.getTxtUser().getText());
            properties.setProperty("hibernate.connection.password", configView.getTxtPass().getText());
            OutputStream output = null;
            try {
                output = new FileOutputStream("./service/Hibernate.properties");
                properties.store(output, null);
                frame.setProperties(properties);
                frame.setIsConnected(false);
                fireIsConnect();
            } catch (FileNotFoundException ex) {
                System.out.println(ex.getMessage());
            } catch (IOException ex) {

            }

        }
    }

}
